﻿Imports System.Web
Imports System.Web.Services
Imports System.IO

Public Class paymentNotification
    Implements System.Web.IHttpHandler

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property
    Private Const AMPERSAND As String = "&"
    Private Const eq As String = "="
    Private Shared ReadOnly AMPERSAND_CHAR_ARRAY As Char() = AMPERSAND.ToCharArray()
    Private Shared ReadOnly EQUALS_CHAR_ARRAY As Char() = eq.ToCharArray()
    Public Function getDateTime() As String
        Return " DateTime: " & DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss")
    End Function
    Sub ProcessRequest(ByVal ctx As HttpContext) Implements IHttpHandler.ProcessRequest
        '
        '			 * set to true to write to individual log files.
        '			 * 
        '			 * 

        Const INDIVIDUAL_LOGS As Boolean = False
        Dim logfilepath As String = Path.Combine("@c:\test.txt")
        Using logfile As New StreamWriter(logfilepath, True)
            Try
                Using reader As New StreamReader(ctx.Request.InputStream)

                    Dim incomingIpn As String = reader.ReadToEnd()
                    logfile.WriteLine("*********************************************************************************************************************************************")
                    '
                    '						*  Listen to the incoming IPN from  paypal server and record it to the log file with timestamp.
                    '						* 
                    '						* 

                    logfile.WriteLine(" ")
                    logfile.WriteLine("Incoming IPN:" & getDateTime())
                    logfile.WriteLine("=============")
                    For Each nvp As String In incomingIpn.Split(AMPERSAND_CHAR_ARRAY)
                        Dim tokens As String() = nvp.Split(EQUALS_CHAR_ARRAY)
                        If tokens.Length >= 2 Then
                            Dim name As String = HttpUtility.UrlDecode(tokens(0))
                            Dim valuepair As String = HttpUtility.UrlDecode(tokens(1))
                            logfile.WriteLine(name & " : " & valuepair)
                        End If
                    Next
                    '
                    '						* IPN post back to the paypal server for validation and record it to the log file with timestamp.
                    '						* 
                    '						* 

                    Dim ipnPostback As String = incomingIpn & "&cmd=_notify-validate"
                    Dim ipnResponse As String = Utils.HttpPost(Constants.WEBSCR_URL, ipnPostback)
                    logfile.WriteLine(" ")

                    logfile.WriteLine("IPN Postback:" & getDateTime())
                    logfile.WriteLine("=============")
                    For Each Ipnnvp As String In ipnPostback.Split(AMPERSAND_CHAR_ARRAY)
                        Dim Ipntokens As String() = Ipnnvp.Split(EQUALS_CHAR_ARRAY)
                        If Ipntokens.Length >= 2 Then
                            Dim Ipnname As String = HttpUtility.UrlDecode(Ipntokens(0))
                            Dim Ipnvaluepair As String = HttpUtility.UrlDecode(Ipntokens(1))
                            logfile.WriteLine(Ipnname & " : " & Ipnvaluepair)
                        End If
                    Next
                    '
                    '						* Record the Ipn Response to the log file with timestamp.
                    '						* 
                    '						* 

                    logfile.WriteLine(" ")
                    'logfile.WriteLine("IPN Response: " & ipnResponse & getDateTime())
                    logfile.WriteLine(" ")
                End Using
            Catch ex As Exception
                logfile.WriteLine(ex.ToString())
            End Try
        End Using
    End Sub
End Class